package cn.jzk.mySpringSecurity_noSeparation.Entity;

import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 登录用户响应类
 * @Author jiangzk
 * @Date 2025/1/22 23:45
 */
@Data
@NoArgsConstructor
public class LoginUser implements UserDetails {

    private User user;

    // 当前用户权限列表信息
    private List<String> securityMsgs;

    // 修改构造函数以接收 List<String> 类型的权限信息
    public LoginUser(User user, List<String> securityMsgs) {
        this.user = user;
        // 防御性拷贝
        this.securityMsgs = new ArrayList<>(securityMsgs);
    }

    // 将权限信息交给 security 所管理
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        // 将 String 转换为 GrantedAuthority
        return this.securityMsgs.stream()
                .map(SimpleGrantedAuthority::new)
                .collect(Collectors.toList());
    }

    @Override
    public String getPassword() {
        return user.getPassword();
    }

    @Override
    public String getUsername() {
        return user.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
